package org.jeecg.modules.mtic.core.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.mtic.core.entity.Config;
import org.jeecg.modules.mtic.core.entity.ConfigBo;
import org.jeecg.modules.mtic.core.service.IConfigService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import java.util.Objects;

 /**
 * @Description: core_config
 * @Author: jeecg-boot
 * @Date:   2021-05-21
 * @Version: V1.0
 */
@Api(tags="core_config")
@RestController
@RequestMapping("/core/config")
@Slf4j
public class ConfigController extends JeecgController<Config, IConfigService> {
	@Autowired
	private IConfigService configService;
	
	/**
	 * 分页列表查询
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "core_config-分页列表查询")
	@ApiOperation(value="core_config-分页列表查询", notes="core_config-分页列表查询")
	//构建查询api参数
@ApiImplicitParams({
            @ApiImplicitParam(value = "租户ID", name = "tenantId", dataType = "Integer") ,
            @ApiImplicitParam(value = "应用ID", name = "appId", dataType = "Integer") ,
            @ApiImplicitParam(value = "标识码", name = "code", dataType = "String") ,
            @ApiImplicitParam(value = "名称", name = "name", dataType = "String") ,
            @ApiImplicitParam(value = "数据类型[字典-dataType]", name = "dataType", dataType = "Integer") ,
            @ApiImplicitParam(value = "标签", name = "tags", dataType = "String") ,
            @ApiImplicitParam(value = "数据源字典类型ID", name = "dictTypeId", dataType = "Integer") ,
            @ApiImplicitParam(value = "禁用[1-禁用,0-启用]", name = "isDisabled", dataType = "Integer") ,
    })	@GetMapping(value = "/")
	public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="15") Integer pageSize,
								   HttpServletRequest req) {
		Config config = new Config();

		QueryWrapper<Config> queryWrapper = QueryGenerator.initQueryWrapper(config, req.getParameterMap());
		Page<Config> page = new Page<Config>(pageNo, pageSize);
		IPage<Config> pageList = configService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param config
	 * @return
	 */
	@AutoLog(value = "core_config-添加")
	@ApiOperation(value="core_config-添加", notes="core_config-添加")
	@PostMapping(value = "/")
	public Result<?> add(@RequestBody Config config) {
		configService.save(config);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param config
	 * @return
	 */
	@AutoLog(value = "core_config-编辑")
	@ApiOperation(value="core_config-编辑", notes="core_config-编辑")
	@PutMapping(value = "/{id}")
	public Result<?> edit(
						@PathVariable(name="id",required=true) String id,
						@RequestBody Config config) {

		//如果实体数据中带有ID且与id参数不同，则会被认为参数不合法。
		Long updId = config.getId();

		if (updId != null && updId > 0) {
			if (Objects.equals(id, updId.toString())) {
				configService.updateById(config);
				return Result.OK("编辑成功!");
			}
		}

		return Result.error("参数非法!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "core_config-通过id删除")
	@ApiOperation(value="core_config-通过id删除", notes="core_config-通过id删除")
	@DeleteMapping(value = "/{id}")
	public Result<?> delete(@PathVariable(name="id",required=true) String id) {
		configService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "core_config-批量删除")
	@ApiOperation(value="core_config-批量删除", notes="core_config-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.configService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "core_config-通过id查询")
	@ApiOperation(value="core_config-通过id查询", notes="core_config-通过id查询")
	@GetMapping(value = "/{id}")
	public Result<?> queryById(@PathVariable(name="id",required=true) String id) {
		Config config = configService.getById(id);
		if(config==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(config);
	}



}
